home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / OrganicButtonBorder.java < prev    next >
Text File  |  1998-06-30  |  5KB  |  138 lines

  1. /*
  2.  * @(#)OrganicButtonBorder.java    1.4 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  */
  20.  
  21. package com.sun.java.swing.plaf.organic;
  22.  
  23. import com.sun.java.swing.*;
  24. import com.sun.java.swing.border.*;
  25. import com.sun.java.swing.plaf.basic.*;
  26. import java.awt.*;
  27. import java.awt.event.*;
  28. import com.sun.java.swing.plaf.*;
  29.  
  30. /**
  31.  * OrganicButtonBorder implementation
  32.  *  This is the border that "normal" buttons use.
  33.  * <p>
  34.  * Warning: serialized objects of this class will not be compatible with
  35.  * future swing releases.  The current serialization support is appropriate
  36.  * for short term storage or RMI between Swing1.0 applications.  It will
  37.  * not be possible to load serialized Swing1.0 objects with future releases
  38.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  39.  * baseline for the serialized form of Swing objects.
  40.  *
  41.  * @version 1.4 02/02/98
  42.  * @author Michael C. Albers
  43.  * @author Tom Santos
  44.  */
  45. public class OrganicButtonBorder extends AbstractBorder {
  46.  
  47.   protected Color getTopLeftColor()            { return UIManager.getColor("Button.highlight"); }
  48.   protected Color getBottomRightColor()        { return UIManager.getColor("Button.shadow"); }
  49.   protected Color getPressedTopLeftColor()     { return UIManager.getColor("Button.shadow"); }
  50.   protected Color getPressedBottomRightColor() { return UIManager.getColor("Button.highlight"); }
  51.   protected Color getDisabledColor()           { return UIManager.getColor("Button.disabled"); }
  52.   
  53.   protected int getSafetyBorderWidth() { return 1; }
  54.   protected int getDefaultBorderWidth() { return 1; }
  55.  
  56.   public void paintBorder(Component c, Graphics g, int x, int y, 
  57.               int w, int h) {
  58.     Color downRight;
  59.     Color upLeft;
  60.     Color safetyB = UIManager.getColor("Button.background");
  61.     Color defaultB = safetyB;
  62.     Color dots = defaultB;
  63.     boolean isPressed = false;
  64.     boolean hasFocus = false;
  65.     boolean isDisabled = false;
  66.     boolean isDefault = false;
  67.     boolean isArmed = false;
  68.     
  69.     if (c instanceof AbstractButton) {
  70.       AbstractButton b = (AbstractButton)c;
  71.       ButtonModel model = b.getModel();
  72.       
  73.       isPressed = (model.isPressed() && model.isArmed());
  74.       hasFocus = (model.isArmed() && isPressed) || 
  75.               (b.isFocusPainted() && b.hasFocus());
  76.       isDisabled = !model.isEnabled();
  77.       isArmed = model.isArmed();
  78.     }
  79.  
  80.     if ( !isDisabled && (hasFocus && !isPressed) ) {
  81.     defaultB = UIManager.getColor("Button.focus");
  82.     }
  83.  
  84.     // Set the colors
  85.     if ( isDisabled ) {
  86.         upLeft = getDisabledColor();
  87.     downRight = getDisabledColor();
  88.     }
  89.     else if ( isPressed && isArmed ) {
  90.         upLeft = getPressedTopLeftColor();
  91.     downRight = getPressedBottomRightColor();
  92.     }
  93.     else {
  94.         upLeft = getTopLeftColor();
  95.     downRight = getBottomRightColor();
  96.     }
  97.  
  98.     // DRAW
  99.     //  For visual differentiation regardless of background
  100.     int sB = getSafetyBorderWidth(); //sB = SafetyBorder for width of visual differentiation
  101.  
  102.     if ( sB > 0 ) {
  103.       g.setColor(safetyB);
  104.       g.drawLine(x,y, x+w-1,y); // UL -> UR
  105.       g.drawLine(x,y, x,y+h-1); // UL -> BL
  106.       g.drawLine(x+1,y+h-1, x+w-1,y+h-1); // BL -> BR
  107.       g.drawLine(x+w-1,y, x+w-1,y+h-1); // UR -> BR
  108.     }
  109.  
  110.     int dB = getDefaultBorderWidth(); //dB = DefaultBorder for width
  111.     if ( dB > 0 ) {
  112.       g.setColor(defaultB);
  113.       g.drawLine(x+sB,y+sB, x+w-1-sB,y+sB); // UL -> UR
  114.       g.drawLine(x+sB,y+sB, x+sB,y+h-1-sB); // UL -> BL
  115.       g.drawLine(x+1+sB,y+h-1-sB, x+w-1-sB,y+h-1-sB); // BL -> BR
  116.       g.drawLine(x+w-1-sB,y+sB, x+w-1-sB,y+h-1-sB); // UR -> BR
  117.     }
  118.  
  119.     int tB = sB + dB; //tB is the total width of the borders
  120.     g.setColor(upLeft); // top and left edge
  121.     g.drawLine(x+tB,y+tB, x+w-tB-1,y+tB); // UL -> UR
  122.     g.drawLine(x+tB,y+tB, x+tB,y+h-tB-1); // UL -> BL
  123.     
  124.     g.setColor(downRight); // bottom and right
  125.     g.drawLine(x+tB,y+h-tB-1, x+w-tB-1,y+h-tB-1); // BL -> BR
  126.     g.drawLine(x+w-tB-1,y+tB, x+w-tB-1,y+h-tB-1); // UR -> BR
  127.     
  128.     g.setColor(dots); // dots in corners
  129.     g.drawLine(x+tB,y+h-tB-1, x+tB,y+h-tB-1);  // downLeft dot
  130.     g.drawLine(x+w-tB-1,y+tB, x+w-tB-1,y+tB);  // upRight dot
  131.   }
  132.   
  133.   public Insets getBorderInsets(Component c)       {
  134.     return new Insets(3, 3, 3, 3);
  135.   }
  136. }
  137.  
  138.